package org.loofer.ext.utils;

import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import org.loofer.ext.component.logger.Logger;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;

public class PerformanceUtils {
    private static final String TAG = "PerformanceUtils";
    private static int sCoreNum = 0;
    private static long sTotalMemo = 0;

    public static int getCpuCores() {
        if (sCoreNum == 0) {
            try {
                sCoreNum = new File("/sys/devices/system/cpu/").listFiles(new FileFilter() {
                    public boolean accept(File pathname) {
                        return Pattern.matches("cpu[0-9]", pathname.getName());
                    }
                }).length;
            } catch (Exception e) {
                Logger.e(TAG, "getCpuCores exception occurred, e=", e);
                sCoreNum = 1;
            }
        }
        return sCoreNum;
    }

    public static long getFreeMemory(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService("activity");
        MemoryInfo mi = new MemoryInfo();
        am.getMemoryInfo(mi);
        return mi.availMem;
    }

    public static long getTotalMemory() {
        IOException e;
        Throwable th;
        if (sTotalMemo == 0) {
            long initial_memory = -1;
            FileReader localFileReader = null;
            try {
                FileReader localFileReader2 = new FileReader("/proc/meminfo");
                try {
                    BufferedReader localBufferedReader = new BufferedReader(localFileReader2, 8192);
                    String str2 = localBufferedReader.readLine();
                    if (str2 != null) {
                        initial_memory = (long) (Integer.valueOf(str2.split("\\s+")[1]).intValue() * 1024);
                    }
                    localBufferedReader.close();
                    if (localFileReader2 != null) {
                        try {
                            localFileReader2.close();
                            localFileReader = localFileReader2;
                        } catch (IOException e2) {
                            Logger.e(TAG, "close localFileReader Exception occurred, e = ", e2);
                            localFileReader = localFileReader2;
                        }
                    }
                } catch (IOException e3) {
                    e2 = e3;
                    localFileReader = localFileReader2;
                    try {
                        Logger.e(TAG, "getTotalMemory Exception occurred, e =", e2);
                        if (localFileReader != null) {
                            try {
                                localFileReader.close();
                            } catch (IOException e22) {
                                Logger.e(TAG, "close localFileReader Exception occurred, e = ", e22);
                            }
                        }
                        sTotalMemo = initial_memory;
                        return sTotalMemo;
                    } catch (Throwable th2) {
                        th = th2;
                        if (localFileReader != null) {
                            try {
                                localFileReader.close();
                            } catch (IOException e222) {
                                Logger.e(TAG, "close localFileReader Exception occurred, e = ", e222);
                            }
                        }
                        throw th;
                    }
                } catch (Throwable th3) {
                    th = th3;
                    localFileReader = localFileReader2;
                    if (localFileReader != null) {
                        localFileReader.close();
                    }
                    throw th;
                }
            } catch (IOException e4) {
                e222 = e4;
                Logger.e(TAG, "getTotalMemory Exception occurred, e =", e222);
                if (localFileReader != null) {
                    localFileReader.close();
                }
                sTotalMemo = initial_memory;
                return sTotalMemo;
            }
            sTotalMemo = initial_memory;
        }
        return sTotalMemo;
    }
}
